home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / StreamBuf.h < prev    next >
C/C++ Source or Header  |  1992-05-20  |  3KB  |  94 lines

  1. #ifndef StreamBuf_First
  2. #ifdef __GNUG__
  3. //pragma once
  4. #pragma interface
  5. #endif
  6. #define StreamBuf_First
  7.  
  8. #include "Types.h"
  9.  
  10. #ifndef BUFSIZE
  11. #define BUFSIZE 4096
  12. #endif
  13.  
  14. #define zapeof(c) ((c) & 0377)
  15.  
  16. class StreamBuf {       // a buffer for streams
  17. protected:
  18.     StreamBuf *sb;      // base streambuf
  19.     char*   base;       // pointer to beginning of buffer
  20.     char*   ptr;       // pointer to next free byte
  21.     char*   gptr;       // pointer to next free byte
  22.     char*   eptr;       // pointer to first byte following buffer
  23.     bool    alloc;      // true if buffer is allocated using "new"
  24.     long fpos;
  25.  
  26. public:
  27.     StreamBuf(int size= BUFSIZE, char *p= 0, int count= 0, bool alloc= FALSE);
  28.     virtual ~StreamBuf();
  29.     
  30.     void Finish();
  31.     void SetBaseStream(StreamBuf *s);
  32.     StreamBuf *GetBaseStream();
  33.  
  34.     // supply an area for a buffer.
  35.     // The "count" parameter allows the buffer to start in non-empty.
  36.     StreamBuf *setbuf(char *p, int len, int count= 0, bool alloc= FALSE);
  37.     
  38.     // return size of buffer
  39.     u_long size()
  40.     { return (u_long) (eptr-base); }
  41.     
  42.     // Empty a buffer. Return EOF on error; 0 on success
  43.     virtual int overflow(int c= EOF);
  44.  
  45.     // Fill a buffer. Return EOF on error or end of input; next character on success
  46.     virtual int underflow();
  47.     
  48.     virtual int Underflow(u_char *bp, int size);
  49.     virtual int Overflow(u_char *bp, int size);
  50.     virtual void Close();
  51.     virtual long Seek(long pos, int whence);
  52.  
  53.     int seek(long pos, bool relative= FALSE);
  54.  
  55.     long tell()
  56.     { return fpos + (long) (ptr-base); }
  57.    
  58.     char *Base()
  59.     { return base; }
  60.     
  61.     int sgetc()        // get the current character
  62.     { return (ptr >= gptr) ? underflow() : zapeof(*ptr); }
  63.  
  64.     int sbumpc()    // get the current character and advance to the next character
  65.     { return (ptr >= gptr) ? underflow() : zapeof(*ptr++); }
  66.  
  67.     int snextc()             // get the next character
  68.     { return (ptr >= (gptr-1)) ? underflow() : zapeof(*++ptr); }
  69.  
  70.     inline void stossc()            // advance to the next character
  71.     { if (ptr++ >= gptr) underflow(); }
  72.  
  73.     // Return a character to the buffer (ala lookahead 1).  Since
  74.     // the user may be "playing games" the character might be 
  75.     // different than the last one returned by sgetc or snextc.
  76.     // been extracted, nothing will be put back.
  77.     // Putting back an EOF is DANGEROUS.
  78.     void sputbackc(char c);
  79.  
  80.     // put a character into the buffer
  81.     inline int sputc(int c= EOF)
  82.     { return (eptr <= ptr) ? overflow(zapeof(c)) : (*ptr++= zapeof(c)); }
  83.  
  84.     // put n characters into the buffer
  85.     int sputn(const char *buf, int n);
  86.  
  87.     // get n characters from the buffer
  88.     int sgetn(char *buf, int n);
  89.     
  90.     int salloc(char **buf, int size);
  91. };
  92.  
  93. #endif
  94.